using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class app_frame_Default2 : System.Web.UI.Page
{
    HDDatabase HDD;
    static DataTable dtHelpCat = null;
    static DataTable dtHelp = null;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        //code modified by Sampath on 12-Jan-2011 for session expiry handling
        if (Session["Theme"] != null)
        {
            if (Session["Theme"].ToString() != "")
                this.Theme = Session["Theme"].ToString();
        }
        if (Session["pageTitle"] != null)
        {
            if (Session["pageTitle"].ToString() != "")
                this.Title = Session["pageTitle"].ToString() + " Help";
        }

        //this.Theme = Session["Theme"].ToString();
        //this.Title = Session["pageTitle"].ToString() + " Help";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //code added by Sampath on 12-Jan-2011 for session expiry handling
        security hdds = new security();
        if (Session["userid"] == null)
            Response.Redirect(hdds.SessionExpiryURL, true);
        if (Session["userid"].ToString() == "")
            Response.Redirect(hdds.SessionExpiryURL, true);

        HDD = new HDDatabase();
        if (!IsPostBack)
        {
            loadHelp();

            if (Request.QueryString["h_id"] != null)
                if (Request.QueryString["h_id"] != "")
                    displayHelp(Request.QueryString["h_id"].ToString());
        }
    }

    private void changeVisible()
    {
        Menu mnu = (Menu)this.Master.FindControl("");
    }

    private void addChildNode(TreeNode tnParent, string sHelpCat)
    {
        try
        {
            TreeNode tnNode = new TreeNode(sHelpCat, sHelpCat);
            foreach (DataRow drChild in dtHelp.Select("help_category ='" + sHelpCat + "'","help_seq"))
                tnNode.ChildNodes.Add(new TreeNode(drChild["help_title"].ToString(), drChild["help_id"].ToString()));
            if (tnNode.ChildNodes.Count > 0)
                tnParent.ChildNodes.Add(tnNode);
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog(ex.Message);
        }
    }

    private void loadHelp()
    {
        try
        {
            string sSql = "Select * from sy_help_category";
            dtHelpCat = HDD.GetDataTable(sSql);

            sSql = "Select * from sy_help";
            dtHelp = HDD.GetDataTable(sSql);
            TreeNode tnRoot = new TreeNode("Help");
            
            tvwHelp.Nodes.Add(tnRoot);

            foreach (DataRow dr in dtHelpCat.Select("hc_of_category is null","help_cat_seq"))
            {
                string sHelpCat = dr["hc_name"].ToString();
                addChildNode(tnRoot, sHelpCat);
            }

            foreach (DataRow dr in dtHelpCat.Select("hc_of_category is not null","help_cat_seq"))
            {
                string sHelpCat = dr["hc_name"].ToString();
                string sHelpSubCat = dr["hc_of_category"].ToString();

                if (sHelpSubCat != "")
                {
                    foreach (TreeNode tn in tnRoot.ChildNodes)
                    {
                        if (tn.Value == sHelpSubCat)
                        {
                            addChildNode(tn, sHelpCat);
                            break;
                        }
                    }
                }
            }
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog(ex.Message);
        }
    }

    protected void tvwHelp_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView tn = (TreeView)sender;

        displayHelp(tn.SelectedValue);
        /*DataRow[] dr = dtHelp.Select("help_id='" + tn.SelectedValue + "'");

        if (dr.Length > 0)
        {
            lblTitle.Text = dr[0]["help_title"].ToString();
            lblDesc.Text = dr[0]["help_desc"].ToString();
        }*/
    }

    private void displayHelp(string Help_id)
    {
        if (dtHelp.Columns.Contains("help_id"))
        {
            DataRow[] dr = dtHelp.Select("help_id='" + Help_id + "'");

            if (dr.Length > 0)
            {
                lblTitle.Text = dr[0]["help_title"].ToString();
                lblDesc.Text = dr[0]["help_desc"].ToString();
            }
        }
    }
}
